<template>
  <div class="main">
    <div class="register_show">
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
        style="width: 70%"
      >
        <el-form-item label="用户名"  prop="username">
          <el-input v-model="ruleForm.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="身份id"  prop="identityId">
          <el-input
            placeholder="请输入学号或职工号，若没有系统会为您分配"
            v-model="ruleForm.identityId"
           autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="pass">
          <el-input
            type="password"
            v-model="ruleForm.pass"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPass">
          <el-input
            type="password"
            v-model="ruleForm.checkPass"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input autocomplete="off" v-model="ruleForm.age"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input autocomplete="off" v-model="ruleForm.email"></el-input>
        </el-form-item>
        <el-form-item label="QQ" prop="qq">
          <el-input autocomplete="off" v-model="ruleForm.qq"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-select placeholder="请选择性别" v-model="ruleForm.sex">
            <el-option label="男" value="man"></el-option>
            <el-option label="女" value="woman"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input autocomplete="off" v-model="ruleForm.phone"></el-input>
        </el-form-item>
        
        <el-form-item label="验证码" prop="phone">
          <el-input autocomplete="off" v-model="ruleForm.verify" style="width: 30%"></el-input>
          <div class="spilter"></div>
          <el-button
            class="button_verify"
            type="primary"
            @click="sendVerify"
            >发送验证码</el-button
          >
        </el-form-item>
        <el-form-item>
          <el-button
            class="button_content_submit"
            type="primary"
            @click="submitForm('ruleForm')"
            >提交</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    var checkAge = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("年龄不能为空"));
      }
      setTimeout(() => {
        if (!parseFloat(value).toString() == "NaN") {
          callback(new Error("请输入数字值"));
        } else {
          if (value < 18) {
            callback(new Error("必须年满18岁"));
          } else {
            callback();
          }
        }
      }, 1000);
    };
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    var validateUsername = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入用户名"));
      } else {
        callback();
      }
    };
    var validateIdentityId= (rule, value, callback) => {
      
        callback();
      
    };
    return {
      ruleForm: {
        pass: "",
        checkPass: "",
        age: "",
        username: "",
        identityId: "",
        sex: "",
        age: "",
        qq: "",
        email: "",
        phone: "",
        verify: "",
      },
      rules: {
        pass: [{ validator: validatePass, trigger: "blur" }],
        checkPass: [{ validator: validatePass2, trigger: "blur" }],
        age: [{ validator: checkAge, trigger: "blur" }],
        username: [{ validator: validateUsername, trigger: "blur" }],
        identityId: [{ validator: validateIdentityId, trigger: "blur" }],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          alert("submit!");
          this.$axios({
            method: "POST",
            url: "http://81.70.192.34:8088/register?verify="+this.ruleForm.verify,
            
            data:{
              username: this.ruleForm.username,
              password: this.ruleForm.pass,
              identityId: this.ruleForm.identityId,
              email: this.ruleForm.email,
              age: this.ruleForm.age,
              qq: this.ruleForm.qq,
              sex: this.ruleForm.sex,
              phone: this.ruleForm.phone,
            },
          })
            .then((res) => {
              console.log(res);
            })
            .catch((error) => {
              console.error(error);
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    sendVerify() {
    console.log("send")
    this.$axios({
      method: "GET",
      url: "http://81.70.192.34:8088/getEmailVerifycode?email="+this.ruleForm.email,
    })
      .then((res) => {
        console.log(res)
      })
      .catch((error) => {
        console.error(error);
      });
  },
  },
  
};
</script>

<style>
.main {
  height: 100vh;
  width: 100vw;
  display: flex;
  flex-wrap: wrap;
}
.register_show {
  height: 90vh;
  width: 50vw;
  margin: auto;
  border-radius: 4px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.12), 0 0 6px rgba(0, 0, 0, 0.04);
}
.demo-ruleForm {
  margin-top: 5vh;
  margin-left: 5vw;
}
.button_content_submit {
  margin-left: auto;
}
.button_verify{
  margin-left: 3vw;
}
.spilter{
  width: 3vw;
}
</style>